﻿using System.Collections.Generic;
using System.IO;

namespace DigitClassification
{
    public abstract class BaseClassifier
    {
        protected int Dim;

        protected List<DigitData> Training = new List<DigitData>();

        protected List<DigitData> GetDigitData(string dataFile, string labelFile)
        {
            var digitDataList = new List<DigitData>();

            Dim = 28;
            using (var dataText = new StreamReader(dataFile))
            using (var labelsText = new StreamReader(labelFile))
            {
                var line = labelsText.ReadLine();
                while (line != null)
                {
                    var digit = int.Parse(line);
                    var attributes = ReadAttributes(dataText);

                    var digitAttr = new DigitData(digit, attributes);
                    digitDataList.Add(digitAttr);

                    line = labelsText.ReadLine();
                }
            }

            return digitDataList;
        }

        private char[,] ReadAttributes(StreamReader dataText)
        {
            var cells = new char[Dim, Dim];
            for (int i = 0; i < 28; i++)
            {
                var l = dataText.ReadLine();
                if (l == null) break;

                var arr = l.ToCharArray();
                for (int j = 0; j < arr.Length; j++)
                    cells[i, j] = arr[j];
            }
            return cells;
        }

        protected bool IsDark(char c)
        {
            return c == '#' || c == '+';
        }
    }
}